/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import me.zhengjie.modules.enrstu.domain.GuardianInformation;
import me.zhengjie.modules.enrstu.service.GuardianChildrenService;
import me.zhengjie.modules.enrstu.domain.GuardianChildren;
import me.zhengjie.modules.enrstu.repository.GuardianChildrenRepository;
import me.zhengjie.modules.enrstu.service.GuardianInformationService;
import me.zhengjie.modules.enrstu.service.dto.GuardianInformationDto;
import me.zhengjie.modules.enrstu.service.mapstruct.GuardianChildrenMapper;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyx
* @date 2024-11-04
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class GuardianChildrenServiceImpl implements GuardianChildrenService {

    private final GuardianChildrenRepository guardianChildrenRepository;
    private final GuardianChildrenMapper guardianChildrenMapper;
    private final GuardianInformationService guardianInformation;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void create(String childrenId, String guardianId) {
        GuardianChildren resources = new GuardianChildren();
        resources.setChildrenId(childrenId);
        resources.setGuardianId(guardianId);
        resources.setId(IdUtil.simpleUUID()); 
        guardianChildrenRepository.save(resources);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(GuardianChildren resources) {
        GuardianChildren guardianChildren = guardianChildrenRepository.findById(resources.getId()).orElseGet(GuardianChildren::new);
        ValidationUtil.isNull( guardianChildren.getId(),"GuardianChildren","id",resources.getId());
        guardianChildren.copy(resources);
        guardianChildrenRepository.save(guardianChildren);
    }

    public void deleteAll(String[] ids) {
        for (String id : ids) {
            guardianChildrenRepository.deleteById(id);
        }
    }


    @Override
    public List<GuardianInformationDto> findGuardianChildrenById(String childrenId) {
        return guardianChildrenRepository.findByChildrenId(childrenId).stream().map(guardianChildren -> {
            return guardianInformation.findById(guardianChildren.getGuardianId());
        }).collect(Collectors.toList());
    }

}